/*
 * Copyright 2018 the original author or authors.
 * 
 * you can use the file or import project.but Do not take legal responsibility.
 * Only for learning communication.
 *
 * Java compiled version 1.8 or higher
 * 
 * 
 * 
 * 
 */
 
package org.demo.util.extend;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

/**
 * 扫描指定的java包下代码，并添加或者替换原有代码源文件头信息
 * 
 * @author jx-air
 * @date 2018年2月24日 上午11:08:28
 * @version 1.0
 */
public class AddHeader {

	private static String header = null;

	public static void main(String[] args) {
		// 路径
		String path = "D:\\workspace\\demo\\demo-base\\demo-util\\src\\main\\java";// 扫描路径
		String pg = "org.demo.util";// 扫描包
		scan(path, pg);

	}

	static {
		header = getHealder();
	}

	private static void scan(String path, String pg) {
		String ph = path + "\\" + pg.replace(".", "\\");
		Path p = Paths.get(ph);
		try {
			// 创建过滤器
			FindJavaVisitor visitor = new FindJavaVisitor("java");
			Files.walkFileTree(p, visitor);//递归获取文件

			List<Path> list = visitor.result;

			for (Path path2 : list) {
				addHeaderOrModify(path2);
				System.out.println(path2);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void addHeaderOrModify(Path path) {
		try {
			List<String> list = Files.readAllLines(path);

			boolean isRead = false;
			BufferedWriter writer = Files.newBufferedWriter(path);
			writer.write(header);

			for (String str : list) {
				if (str != null && str.indexOf("package") > -1) {
					isRead = true;
				}
				if (isRead) {
					writer.write(str);
					writer.newLine();
				}
			}

			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static String getHealder() {
		StringBuffer sb = new StringBuffer();
		InputStream inputStream = AddHeader.class.getResourceAsStream("header.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

		String line = null;
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line).append("\r\n");
			}

			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	/**
	 * 
	 * 添加文件访问器，过滤.java文件
	 */
	private static class FindJavaVisitor extends SimpleFileVisitor<Path> {
		private List<Path> result = new ArrayList<Path>();
		private String suffix;

		public FindJavaVisitor(String suffix) {
			this.suffix = suffix;
		}

		@Override
		public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
			if (file.toString().endsWith("." + suffix)) {
				result.add(file);
			}
			return FileVisitResult.CONTINUE;
		}
	}
}
